$(function() {
	init();
	initData();
});


function initData() {
	var id = location.hash;
	var ids = id.substring(1, id.length).split(',');
	var gamePrizeId = ids[0];
	$.getJSON('/fw/game/prize_getbyid.action', {
		"gamePrizeId" : gamePrizeId
	}, function(data) {
		$("#gamePrizeId").val(gamePrizeId);
		$("#prizeName").val(data.prizeName);
		$("#type").val(data.type);
		$("#prizeRemark").val(data.prizeRemark);
		$("#prizeNum").val(data.prizeNum);
		initgame(data.gameId,data.level);
		
	});
}

function initgame(gameId,level){
	$.getJSON('/fw/game/game_getlist.action',function(data){
		var data = data.rows;
		var str = "<option value=''>请选择游戏</option>";
		for(var i = 0; i < data.length;i++){
			str += "<option value="+data[i].gameId+">"+data[i].gameName+"</option>";
		}
		$('#gameId').html(str);
		if(gameId){
			$('#gameId').val(gameId);
			initgamelevel(gameId,level);
			$('#gameId').change(function(){
				var gameId = $('#gameId').val();
				if(gameId)
					initgamelevel(gameId,level);
			});
		}
	});
}

function initgamelevel(gameId,level){
	$.getJSON('/fw/game/game_getbyid.action',{"gameId":gameId},function(data){
		var gameLevel;
		gameLevel = data.gameLevel;
		if(gameLevel > 0){
			var str = "<option value=''>请选择奖品等级</option>";
			for(var i = 1; i <= gameLevel;i++){
				str += "<option value=" + i + ">" + i + " 等奖</option>";
			}
			$('#level').html(str);
			$('#level').val(level);
		}
	});
}

function successCallBack() {
	window.location.href = 'gamePrizeList.jsp';
}

/**
 * 提示信息处理
 */
function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle( {
		fieldMap : {},
		successMsg : '添加成功！',
		failMsg : '添加失败！错误：',
		success : successCallBack
	});
}


function init() {
	savehandle();
	changeActiveItem('gamePrizeList');
}

function add() {
	var gamePrizeId = $('#gamePrizeId').val();
	if (!gamePrizeId) {
		$('#alertdiv').savehandle('setmsg', {msg : '数据异常!请稍后再试!',timer : 2000});
		return;
	}
	var gameId = $('#gameId').val();
	if (!gameId) {
		$('#alertdiv').savehandle('setmsg', {msg : '请选择游戏!',timer : 2000});
		return;
	}
	var level = $('#level').val();
	if (!level) {
		$('#alertdiv').savehandle('setmsg', {msg : '请选择奖品等级!',timer : 2000});
		return;
	}
	var type = $('#type').val();
	if (!level) {
		$('#alertdiv').savehandle('setmsg', {msg : '请选择奖品等级!',timer : 2000});
		return;
	}
	var probability = $('#probability').val();
	if (!probability) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写中奖概率!',timer : 2000});
		return;
	}
	var prizeName = $('#prizeName').val();
	if (!prizeName) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写奖品名称!',timer : 2000});
		return;
	}
	var prizeRemark = $('#prizeRemark').val();
	if (!prizeRemark) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写奖品内容!',timer : 2000});
		return;
	}
	var prizeNum = $('#prizeNum').val();
	var data = {
			"gamePrizeId" : gamePrizeId,
			"level" : level,
			"prizeName": prizeName,
			"prizeRemark" : prizeRemark,
			"probability" : probability,
			"type" : type,
			"prizeNum" : prizeNum,
			"game.gameId" : gameId
	};
	$.getJSON('/fw/game/prize_edit.action', data, function(data) {
		if (data.success) {
			$('#alertdiv').savehandle('setmsg', {
				msg : '成功!',
				timer : 2000
			});
			successCallBack();
		} else {
			var msg = data.result;
			$('#alertdiv').savehandle('setmsg', {
				msg : msg.errors,
				timer : 2000
			});
		}
	});
}
